'use strict';

const Controller = require('egg').Controller;

class StatisticController extends Controller {
  /**
   * 获取投稿统计信息 根据部门
   * 支持多维度筛选和排序
   * @param {Object} ctx.request.body
   * @param {string} [ctx.request.body.order_by='score'] - 排序方式：score-按分数，count-按数量
   * @param {string} [ctx.request.body.start_date] - 开始日期，格式：YYYY-MM-DD
   * @param {string} [ctx.request.body.end_date] - 结束日期，格式：YYYY-MM-DD
   * @param {number} [ctx.request.body.approved_status=1] - 审核状态
   */
  async getAllContributionStatisticsByDepartment() {
    const {
      ctx
    } = this;
    // 验证参数
    ctx.validate({
      order_by: {
        type: 'enum',
        values: ['score', 'count'],
        required: false
      },
      start_date: {
        type: 'string',
        required: false,
        format: /^\d{4}-\d{2}-\d{2}$/
      },
      end_date: {
        type: 'string',
        required: false,
        format: /^\d{4}-\d{2}-\d{2}$/
      },
      approved_status: {
        type: 'number',
        required: false,
        min: 0,
        max: 2
      }
    });

    const stats = await ctx.service.statistic.getAllContributionStatisticsByDepartment(ctx.request.body);
    ctx.body = stats;
  }

    /**
   * 获取投稿统计信息 根据项目组
   * 支持多维度筛选和排序
   * @param {Object} ctx.request.body
   * @param {string} [ctx.request.body.order_by='score'] - 排序方式：score-按分数，count-按数量
   * @param {string} [ctx.request.body.start_date] - 开始日期，格式：YYYY-MM-DD
   * @param {string} [ctx.request.body.end_date] - 结束日期，格式：YYYY-MM-DD
   * @param {number} [ctx.request.body.approved_status=1] - 审核状态
   */
    async getAllContributionStatisticsByProjectGroup() {
      const {
        ctx
      } = this;
      // 验证参数
      ctx.validate({
        order_by: {
          type: 'enum',
          values: ['score', 'count'],
          required: false
        },
        start_date: {
          type: 'string',
          required: false,
          format: /^\d{4}-\d{2}-\d{2}$/
        },
        end_date: {
          type: 'string',
          required: false,
          format: /^\d{4}-\d{2}-\d{2}$/
        },
        approved_status: {
          type: 'number',
          required: false,
          min: 0,
          max: 2
        }
      });
  
      const stats = await ctx.service.statistic.getAllContributionStatisticsByProjectGroup(ctx.request.body);
      ctx.body = stats;
    }
}

module.exports = StatisticController;